﻿
function OnlyIntByYang() {//调用函数时可以是this.value

    if (event.keyCode == 45 && this.value.indexOf("-") != -1)
        event.returnValue = false;

    event.returnValue=(event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode == 45);

}

//乘法
function Multiplication(v1, v2) {
    var m = 0, s1 = v1.toString(), s2 = v2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.toString().replace(/\,/g, "")) * Number(s2.toString().replace(/\,/g, "")) / Math.pow(10, m)
    
}

//除法
function Division(v1, v2) {
    var rst = 0;
    if (v2 != "0")
        rst = parseFloat(v1.toString().replace(/\,/g, "")) / parseFloat(v2.toString().replace(/\,/g, ""));
    return rst;
}

function AddComma(num) {
    num = num + "";
    num = num.replace(/[ ]/g, "");
    if (num == "") {
        return num;
    }
    if (isNaN(num)) {
        return num;
    }
    //2.针对是否有小数点，分情况处理 
    var index = num.indexOf(".");
    if (index == -1) {//无小数点 
        var reg = /(-?\d+)(\d{3})/;
        while (reg.test(num)) {
            num = num.replace(reg, "$1,$2");
        }
    } else {
        var intPart = num.substring(0, index);
        var pointPart = num.substring(index + 1, num.length);
        var reg = /(-?\d+)(\d{3})/;
        while (reg.test(intPart)) {
            intPart = intPart.replace(reg, "$1,$2");
        }
        num = intPart + "." + pointPart;
    }
    return num;
}

//加法
function Addition(arg1, arg2) {

    var r1, r2, m, c;

    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }

    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }

    c = Math.abs(r1 - r2);
    m = Math.pow(10, Math.max(r1, r2))
    if (c > 0) {
        var cm = Math.pow(10, c);
        if (r1 > r2) {
            arg1 = Number(arg1.toString().replace(".", ""));
            arg2 = Number(arg2.toString().replace(".", "")) * cm;
        }
        else {
            arg1 = Number(arg1.toString().replace(".", "")) * cm;
            arg2 = Number(arg2.toString().replace(".", ""));
        }
    }
    else {
        arg1 = Number(arg1.toString().replace(".", ""));
        arg2 = Number(arg2.toString().replace(".", ""));
    }
    return (arg1 + arg2) / m

}

//设置默认值
function SetDefaultValue(id, value) {
    var doc = $("#" + id);
    if (doc.val() == "") {
        doc.val(value);
    }
}
//浮点型限制
function OnlyDoubleByYang(str) {//调用函数时可以是this.value
    var ifStart = false; //是否开始输入数字
    var pointNo = 0; //小数点的个数
    if (str.length > 0) {
        ifStart = true;
        if (str.indexOf('.') != -1) {
            pointNo++;
        }
    }
    if ((event.keyCode >= 48) && (event.keyCode <= 57)) {
        ifStart = true;
        //注销的部分保留一位小数
        var strS = new Array();
        strS = str.split(".");
        if (strS.length > 1) {
            if (strS[1].length > 0)
                event.returnValue = false;
            else
                event.returnValue = true;
        }
        event.returnValue = true;
    }
    else if (event.keyCode == 46) {//如果是小数点
        if (ifStart == true && pointNo == 0) {//判断小数点前是不是已经有了数字，并且已经存在的小数点为0
            pointNo++;
            event.returnValue = true;
        }
        else { event.returnValue = false; }
    }
    else { event.returnValue = false; }
}
$(document).ready(function () {
    $("[data-onlyfloat='true']").find(":input").attr("class", "alignRight");
    //浮点型限制
    $("[data-onlyfloat='true']").find(":input").keypress(function () {
        OnlyDoubleByYang($(this).val());
    });
    //
    $("[data-onlyfloat='true']").find(":input").focusin(function () {
        if ($(this).val() == "0") {
            $(this).val("");
        }
    });
    $("[data-onlyfloat='true']").find(":input").focusout(function () {
        if ($(this).val() == "") {
            $(this).val("0");
        }
    });
});


 